package mall

import (
	"errors"
	"github.com/jinzhu/copier"
	"mall-go-gin/global"
	"mall-go-gin/model/common"
	"mall-go-gin/model/mall"
	"mall-go-gin/model/mall/request"
	"time"
)

type MallUserAddressService struct{}

func (m *MallUserAddressService) SaveUserAddress(token string, req request.AddAddressParam) (err error) {
	var userToken mall.MallUserToken
	err = global.GVA_DB.Where("token = ?", token).First(&userToken).Error
	if err != nil {
		return errors.New("不存在的用户")
	}
	// 是否新增默认地址, 将之前的默认地址设置为非默认
	var defaultAddress mall.MallUserAddress
	var address mall.MallUserAddress
	copier.Copy(&defaultAddress, &req)
	defaultAddress.CreateTime = common.JSONTime{Time: time.Now()}
	defaultAddress.UpdateTime = common.JSONTime{Time: time.Now()}
	defaultAddress.UserId = userToken.UserId
	copier.Copy(&address, &defaultAddress)
	if req.DefaultFlag == 1 {
		global.GVA_DB.Where("user_id = ? and default_flag = 1 and is_deleted = 0", userToken.UserId).First(&defaultAddress)
		if defaultAddress != (mall.MallUserAddress{}) {
			defaultAddress.UpdateTime = common.JSONTime{time.Now()}
			defaultAddress.DefaultFlag = 0
			err = global.GVA_DB.Save(&defaultAddress).Error
			if err != nil {
				return err
			}
			defaultAddress.DefaultFlag = int(req.DefaultFlag)
			err = global.GVA_DB.Create(&address).Error
			if err != nil {
				return err
			}

		}
	} else {
		err = global.GVA_DB.Create(&defaultAddress).Error
		if err != nil {
			return err
		}
	}
	return nil
}

// GetMyAddress 获取收货列表
func (m *MallUserAddressService) GetMyAddress(token string) (err error, userAddress []mall.MallUserAddress) {
	var userToken mall.MallUserToken
	err = global.GVA_DB.Where("token = ?", token).First(&userToken).Error
	if err != nil {
		return errors.New("不存在的用户"), userAddress
	}
	err = global.GVA_DB.Where("user_id = ? and is_deleted = 0", userToken.UserId).Find(&userAddress).Error
	return err, userAddress
}

// 查看地址详情
func (m *MallUserAddressService) GetMallUserAddressById(token string, id int) (err error, userAddress mall.MallUserAddress) {
	var userToken mall.MallUserToken
	if err = global.GVA_DB.Where("token = ?", token).First(&userToken).Error; err != nil {
		return errors.New("不存在的用户"), userAddress
	}
	if err = global.GVA_DB.Where("address_id = ?", id).First(&userAddress).Error; err != nil {
		return errors.New("不存在的用户地址"), userAddress
	}
	if userToken.UserId != userAddress.UserId {
		return errors.New("禁止该操作！"), userAddress
	}
	return nil, userAddress
}

// 修改地址
func (m *MallUserAddressService) UpdateUserAddress(token string, req request.UpdateAddressParam) (err error) {
	var userToken mall.MallUserToken
	if err = global.GVA_DB.Where("token = ?", token).First(&userToken).Error; err != nil {
		return errors.New("不存在的用户")
	}
	var userAddress mall.MallUserAddress
	if err = global.GVA_DB.Where("address_id = ? and user_id = ?", req.AddressId, userToken.UserId).First(&userAddress).Error; err != nil {
		return errors.New("不存在的用户地址")
	}
	if userToken.UserId != userAddress.UserId {
		return errors.New("禁止该操作！")
	}
	if req.DefaultFlag == 1 {
		var defaultUserAddress mall.MallUserAddress
		global.GVA_DB.Where("user_id = ? and default_flag = 1 and is_deleted = 0", userToken.UserId).First(&defaultUserAddress)
		if defaultUserAddress != (mall.MallUserAddress{}) {
			defaultUserAddress.DefaultFlag = 0
			defaultUserAddress.UpdateTime = common.JSONTime{time.Now()}
			err = global.GVA_DB.Save(&defaultUserAddress).Error
			if err != nil {
				return err
			}
		}
	}
	if err = copier.Copy(&userAddress, req); err != nil {
		return
	}
	userAddress.UpdateTime = common.JSONTime{time.Now()}
	userAddress.UserId = userToken.UserId
	err = global.GVA_DB.Save(&userAddress).Error
	return err
}

func (m *MallUserAddressService) DeleteUserAddress(token string, id int) (err error) {
	var userToken mall.MallUserToken
	if err = global.GVA_DB.Where("token = ?", token).First(&userToken).Error; err != nil {
		return errors.New("不存在的用户")
	}
	var userAddress mall.MallUserAddress
	if err = global.GVA_DB.Where("address_id = ?", id).First(&userAddress).Error; err != nil {
		return errors.New("不存在的用户地址")
	}
	if userToken.UserId != userAddress.UserId {
		return errors.New("禁止该操作！")
	}

	//err = global.GVA_DB.Delete(&userAddress).Error // 硬删除

	// 软删除
	userAddress.UpdateTime = common.JSONTime{Time: time.Now()}
	userAddress.IsDeleted = 1
	err = global.GVA_DB.Save(&userAddress).Error
	return err
}

func (m *MallUserAddressService) GetMallUserDefaultAddress(token string) (err error, userAddress mall.MallUserAddress) {
	var userToken mall.MallUserToken
	if err = global.GVA_DB.Where("token = ?", token).First(&userToken).Error; err != nil {
		return errors.New("不存在的用户"), userAddress
	}
	if err = global.GVA_DB.Where("user_id = ? and default_flag = 1 and is_deleted = 0", userToken.UserId).First(&userAddress).Error; err != nil {
		return errors.New("不存在默认地址"), userAddress
	}
	return
}
